<!DOCTYPE html>
<html>
<head>
    <title>邮箱发送</title>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
    <link href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css"/>
    <style>
        .email-wrap {
            width: 600px;
            /*height: 700px;*/
            border: 1px solid #ccc;
            box-shadow: 0px 4px 16px 0px rgba(0, 0, 0, 0.1);
            border-radius: 12px;
            margin: auto;
            margin-top: 100px;
            padding: 50px 0;
        }
        .email-box{
            width: 90%;
            margin: 0 auto;
            padding: 0 30px;
            /*background: #4169E1;*/
        }
        .email-Push {
            width: 100%;
            height: 54px;
            line-height: 54px;
            text-align: center;
            font-size: 18px;
            font-weight: bold;
        }

        .email-data {
            width: 90%;
            margin: auto;
            height: 54px;
            display: flex;
            line-height: 54px;
        }

        .email-ok {
            float: right;
            width: 40px;
            height: 20px;
            color: #3B4CD8;
            text-align: center;
            line-height: 16px;
            border: 1px solid #3B4CD8;
            border-radius: 4px;
            margin-right: 20px;
            margin-top: 20px;
        }

        #fti {
            display: inline-block;
            float: right;
            line-height: 0px;
            font-size: 12px;
            color: #FF210B;
        }

        #tti {
            display: inline-block;
            float: left;
            line-height: 0px;
            font-size: 12px;
            color: #FF210B;
        }

        .Send-out {
            display: block;
            width: 90%;
            margin: 0 auto;
            height: 45px;
            line-height: 45px;
            background-color: #3B4CD8;
            color: #ffffff;
            font-size: 14px;
            border-radius: 8px;
        }

        #message {
            width: 86%;
            height: 100px;
            overflow: auto;
            border: 1px solid #3B4CD8;
            border-radius: 12px;
            color: #000;
            font-size: 12px;
            text-align: center;
            line-height: 30px;
        }

        .xinxi {
            /*width: 90%;*/
            /*margin: auto;*/
            height: 100px;
            line-height: 100px;
            margin-top: 10px;
            margin-bottom: 30px;
        }
        .title{
            height: 30px;
            line-height: 30px;
            margin-bottom: 20px;
        }
        .title input{
            width: 75%;
            margin-left: 5%;
        }
        .titletext{
            height: 100px;
            line-height: 100px;
            margin-bottom: 20px;
        }
        .titletext textarea{
            width: 75%;
            margin-left: 5%;
            height: 100px;
        }
        .info{
            margin-right: 20px;
        }
    </style>
</head>
<body>
<div class="email-wrap">
    <div class="email-box">
        <p class="email-Push">邮箱推送</p>
        <div class="email-data">
            <span>发件人邮箱excel:</span>
            <form id="ff" enctype="multipart/form-data">
                <input type="file" id="feml" name="email" accept=".xls , .xlsx"/>
                <input type="button" value="确定" onclick="fEmail();" class="email-ok"/>
                <br>
                <span id="fti"></span>
            </form>
        </div>
        <div class="email-data">
            <span>收件人邮箱excel:</span>
            <form id="tf" enctype="multipart/form-data">
                <input type="file" id="teml" name="email" accept=".xls , .xlsx"/>
                <input type="button" value="确定" onclick="tEmail();" class="email-ok"/>
                <br>
                <span id="tti"></span>
            </form>
        </div>
        <p class="email-data">收件人邮箱数据</p>
        <div>
            <p class="email-data title"><span>标题:</span><input id="title" type="text" name=""/></p>
            <p class="email-data titletext"><span>数据:</span><textarea id="tData"></textarea></p>
            <button onclick="send();" class="Send-out email-data">发送</button>
            <!--<input type="button" value="发送" onclick="test();" class="Send-out"/>-->
            <p class="email-data"><span class="info">发送者邮箱:</span><span id="femailone"></span></p>
            <p class="email-data"><span class="info">收件人邮箱:</span><span id="temailone"></span></p>
            <p class="email-data"><span class="info">发件人当前发送数量:</span><span id="femailcount"></span></p>
            <p class="email-data"><span class="info">当前发送总数:</span><span id="allcount" ></span></p>

        </div>
        <div class="xinxi email-data">
            <span class="info">信息:</span>
            <div id="message">
            </div>
        </div>
    </div>

</div>
</body>
<script type="text/javascript">
    let fromEmail = [];
    console.log("初始化发送者邮箱数组", fromEmail)
    let toEmail = [];
    console.log("初始化收件人邮箱数组", fromEmail)
    let sendIndex = 0;
    console.log("初始化当前邮箱发送数", sendIndex)
    let sendCount = 0;
    console.log("初始化发送总数", sendCount)
    let errorCount = 0;
    console.log("初始化错误总数", errorCount)

    async function send() {
        let title = $("#title").val();
        let tData = $("#tData").val();

        if (!title) {
            alert("标题为空")
        } else if (!tData) {
            alert("数据为空")
        } else if (fromEmail.length <= 0) {
            alert("发送者邮箱为空")
        } else if (toEmail.length <= 0) {
            alert("收件人邮箱为空")
        } else {
            console.log(fromEmail, toEmail)
            let j = 0;
            for (let i = 0; i < toEmail.length;) {
                if(errorCount>10){
                    alert("产生错误过多");
                    break
                }
                console.log(j, i);
                let data = {
                    from: fromEmail[j][0],
                    pass: fromEmail[j][1],
                    to: toEmail[i],
                    subject: title,
                    content: tData
                };
                let ti = `${fromEmail[j][0]}--->>>${toEmail[i]}:::`;
                 await $.ajax({
                    url: "/api/mail/send/email",
                    type: "post",
                    data: data,
                    dataType: 'json',
                    success: function (result) {
                        console.log("aaaaa",result);
                        if (result && result.code) {
                            console.log(result.code);
                            if (result.code + "" === "200") {
                                i++;
                                sendIndex++;
                                sendCount++;
                                ti = ti + "发送成功";
                            } else if (result.code + "" === "1") {
                                sendIndex = 0;
                                j++;
                                ti = ti + "发件人邮箱初始化失败";
                            } else if (result.code + "" === "2") {
                                i++;
                                ti = ti + "收件人邮箱错误";
                            } else if (result.code + "" === "3") {
                                if(sendIndex > 2){
                                    sendIndex = 0;
                                    j++;
                                    ti = ti + "发件人邮箱发送数量达到上限";
                                }else {
                                    errorCount++;
                                    ti = ti + "邮件发送错误";
                                }
                            }
                        } else {
                            ti = ti + "无";
                        }
                        ti = ti + "<br>";
                        $('#femailone').text(fromEmail[j][0]);
                        $('#temailone').text(toEmail[i]);
                        $('#femailcount').text(sendIndex);
                        $('#allcount').text(sendCount);
                        $('#message').append(ti)
                    },
                    error: function () {
                        errorCount++;
                        ti = ti + "系统错误";
                        $('#message').append(ti)
                    }
                });
            }
        }
    }
    function fEmail() {
        let emailData = $("#feml").val();
        if (emailData === '') {
            $('#fti').text('上传内容为空');
        } else {
            var form = new FormData(document.getElementById("ff"));
            $.ajax({
                url: "/api/excel/importEmail?type=f",
                type: "post",
                data: form,
                processData: false,
                contentType: false,
                beforeSend: function () {
                    $('#fti').text('上传中');
                },
                success: function (result) {
                    fromEmail = result.data;
                    $('#fti').text('上传完成');
                    console.log("fromEmail",fromEmail)
                },
                error: function () {
                    $('#fti').text('上传失败');
                }
            });
        }
    }

    function tEmail() {
        let emailData = $("#teml").val();
        if (emailData === '') {
            $('#tti').text('上传内容为空');
        } else {
            var form = new FormData(document.getElementById("tf"));
            $.ajax({
                url: "/api/excel/importEmail?type=t",
                type: "post",
                data: form,
                processData: false,
                contentType: false,
                beforeSend: function () {
                    $('#tti').text('上传中');
                },
                success: function (result) {
                    toEmail = result.data;
                    $('#tti').text('上传完成');
                    console.log("toEmail",toEmail)
                },
                error: function () {
                    $('#tti').text('上传失败');
                }
            });
        }
    }
</script>
</html>
